﻿
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<link href="~/plugins/zTree_v3/css/bootstrapStyle/bootstrapStyle.css" rel="stylesheet" />
<div class="ibox">
    <div class="ibox-content">
        <div class="row">
            <div class="col-md-2">
                <div class="ibox">
                    <div class="ibox-title">栏目列表</div>
                    <div class="ibox-content">
                        <ul id="tplTree" class="ztree"></ul>
                    </div>
                </div>
                
            </div>
            <div class="col-md-9">
                <div id="toolbar" class="btn-group">

                </div>
                <table id="table"></table>
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script src="~/plugins/zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script>
        var $table, pid, lastChannelNode ;
        $(function () {
            loadChannelTree();
            loadGrid();
        });


        function loadChannelTree() {
            var opts = {

                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "pId",
                    }
                }, callback: {
                    onClick: DicZtreeClick

                }, check: {
                    enable: true,
                    chkStyle: "radio"
                }
            };
            jutils.ajaxGet('/Cms/TemplateCode/TemplateCategory', null, function (res) {

                var templates = res.data.templates;
                var defaultModel = { id: 0, name: "全部", pId: 0,open: true, title: '' };
                templates.splice(0, 0, defaultModel);
                tplTree = $.fn.zTree.init($("#tplTree"), opts, res.data.templates);
                tplTree.expandAll(true);
            });
        }

        //单击事件
        function DicZtreeClick(event, treeId, treeNode) {
            if (lastChannelNode) {
                tplTree.checkNode(lastChannelNode, false, true);
            }
            tplTree.checkNode(treeNode, true, true);
            lastChannelNode = treeNode;
            pid = treeNode.id;
            $table.bootstrapTable('refresh');
        }


        function loadGrid() {
            $table = $("#table").bootstrapTable({
                url: '/cms/templatecode/GetData',
                toolbar: '#toolbar',
                pagination: true,
                height: $(window).height() - 50,
                pageSize: 15,
                pageList: [5, 10, 15, 20],
                showColumns: true,
                showRefresh: true,
                clickToSelect: true,
                sidePagination: "server",//
                locale: "zh-CN",
                striped: true,
                queryParams: function (params) {
                    var dto = {
                        limit: params.limit, //每页多少条
                        offset: params.offset, //从第几条开始读(默认就好)
                        pid: pid
                    };
                    return dto;
                },
                columns: [
                    { field: 'template_name', title: '模板名称', align: 'left'},
                    { field: 'template_file', title: '模板文件', align: 'center' },
                    { field: 'file_name', title: '生成文件名', align: 'center' },
                    {
                        field: 'template_mode', title: '类型', align: 'center', formatter: function (v) {
                            if (v === 1) {
                                return '首页模板';
                            } else if (v === 2) {
                                return '栏目模板';
                            } else if (v === 3) {
                                return '内容模板';
                            } else if (v === 4) {
                                return '单页模板';
                            } else if (v === 5) {
                                return '包含文件';
                            }
                            return '';
                        }
                    },
                    {
                        field: 'is_default', title: '默认模板', align: 'center', formatter: function (v) {
                            if (v === 1) {
                                return '<span class="label label-primary">默认模板</span>';
                            }
                            return '';
                        }
                    },
                    {
                        field: 'id', title: '操作', align: 'center',
                        formatter: render
                    }
                ]

            });
            //jutils.initToolBar('#toolbar', true);
            jutils.initToolBarRole('#toolbar');
        }

        function render(v, r, i) {
            var html = jutils.initToolBarRow(v);
            if (r.template_mode === 1) {
                var genHtml = '<a class="btn btn-white btn-bitbucket" title="生成首页" href="javascript:void(0);" onclick="genHome()" data-id="' + v + '"><i class="fa fa-rocket"></i></a>';
                html = genHtml + html;
            }
          
            return html;
        }

        function genHome(obj) {

            jutils.ajaxGet('/content/GenerateHome', null, function (res) {
                if (res.status) {
                    jutils.success("首页生成成功");
                }
            });
        }
        function renderTitle(v, d) {
            return '<a href="/cms/content/' + d.channel_id + '/' + d.id + '.html" target="_blank">' + v + '</a>';
        }
        function add() {
            var url = '/cms/templatecode/Form?id=0';

            parent.$.altasTab.open(
                {
                    id: 'vtemplate',
                    title: '模板设置',
                    url: url,
                    close: function () {
                        $table.bootstrapTable('refresh');
                    }
                });
        }
        function edit(obj) {
            var id = $(obj).data('id');
            var url = '/cms/templatecode/Form?id=' + id;
            //location.href = url;
            parent.$.altasTab.open(
                {
                    id: 'vtemplate',
                    title: '模板设置',
                    url: url,
                    close: function () {
                        $table.bootstrapTable('refresh');
                    }
                });
        }

        function del(obj) {
            var id = $(obj).data('id');
            jutils.confirm("确认删除当前模板吗", function () {
                jutils.ajaxGet('/cms/templatecode/DeleteById', { id: id }, function (res) {
                    $table.bootstrapTable('refresh');
                })
            })
        }

        function copyFile(obj) {
            var id = $(obj).data('id');
            jutils.confirm("确认复制一份新的模板副本吗", function () {
                jutils.ajaxGet('/cms/templatecode/copyFile', { id: id }, function (res) {
                    $table.bootstrapTable('refresh');
                })
            })
        }

        function syncTemplate(obj) {
            var id = $(obj).data('id');
            jutils.confirm("确认本地模板同步到数据库吗", function () {
                jutils.ajaxGet('/cms/templatecode/syncTemplate', { id: id }, function (res) {
                    $table.bootstrapTable('refresh');
                })
            })
        }
    </script>

}
